package com.hydrografix.android.ttrack.model;

import java.io.Serializable;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.text.format.DateFormat;
import android.content.Context;

import com.hydrografix.android.ttrack.DateTimeFormatter;
import com.hydrografix.android.ttrack.R;

public class TimeSlice implements Serializable {
	private static final long serialVersionUID = 6586305797483181442L;

	private int rowId;

	private long startTime = 0;

	private long endTime = 0;

	private TimeSliceCategory category;

	private String notes;

	private static Calendar calendar = new GregorianCalendar();

	public static final long MILLIS_IN_A_DAY = 24 * 60 * 60 * 1000;

	public static final int IS_NEW_TIMESLICE = -1;

	public int getRowId() {
		return rowId;
	}

	public void setRowId(int rowId) {
		this.rowId = rowId;
	}

	public long getDurationInMilliseconds() {
		return endTime - startTime;
	}

	public String getStartDateStr(Context c) {
		if (startTime == 0) {
			return "";
		} else {
			java.text.DateFormat ldf = DateFormat.getLongDateFormat(c); 
			return ldf.format(startTime).toString();
		}
	}

	public String getStartMonthStr(Context c) {
		if (startTime == 0) {
			return "";
		} else {
			return DateFormat.format(c.getText(R.string.disp_monformat), startTime).toString();
		}
	}

	public String getStartWeekStr(Context c) {
		if (startTime == 0) {
			return "";
		} else {
			Calendar ca = new GregorianCalendar();
			ca.setTimeInMillis(startTime);
			int dayOfWeek = ca.get(Calendar.DAY_OF_WEEK);
			long firstDayOfWeekDate = startTime;
			if (dayOfWeek == Calendar.MONDAY) {
				firstDayOfWeekDate -= MILLIS_IN_A_DAY;
			} else if (dayOfWeek == Calendar.TUESDAY) {
				firstDayOfWeekDate -= MILLIS_IN_A_DAY * 2;
			} else if (dayOfWeek == Calendar.WEDNESDAY) {
				firstDayOfWeekDate -= MILLIS_IN_A_DAY * 3;
			} else if (dayOfWeek == Calendar.THURSDAY) {
				firstDayOfWeekDate -= MILLIS_IN_A_DAY * 4;
			} else if (dayOfWeek == Calendar.FRIDAY) {
				firstDayOfWeekDate -= MILLIS_IN_A_DAY * 5;
			} else if (dayOfWeek == Calendar.SATURDAY) {
				firstDayOfWeekDate -= MILLIS_IN_A_DAY * 6;
			}
			java.text.DateFormat ldf = DateFormat.getLongDateFormat(c); 
			return c.getText(R.string.timesl_week_of) + " " + ldf.format(firstDayOfWeekDate).toString();
		}
	}

	public int getStartTimeComponent(int componentId) {
		calendar.setTimeInMillis(startTime);
		return calendar.get(componentId);
	}

	public void setStartTimeComponent(int componentId, int value) {
		calendar.setTimeInMillis(startTime);
		calendar.set(componentId, value);
		startTime = calendar.getTimeInMillis();
	}

	public int getEndTimeComponent(int componentId) {
		calendar.setTimeInMillis(endTime);
		return calendar.get(componentId);
	}

	public void setEndTimeComponent(int componentId, int value) {
		calendar.setTimeInMillis(endTime);
		calendar.set(componentId, value);
		endTime = calendar.getTimeInMillis();
	}

	public String getStartTimeStr(Context c) {
		if (startTime == 0) {
			return "";
		} else {
			int tifo = DateFormat.is24HourFormat(c) ? R.string.disp_timeformat_24 : R.string.disp_timeformat_12;
			return DateFormat.format(c.getString(tifo), startTime).toString();
		}
	}

	public String getEndTimeStr(Context c) {
		if (startTime == 0) {
			return "";
		} else {
			int tifo = DateFormat.is24HourFormat(c) ? R.string.disp_timeformat_24 : R.string.disp_timeformat_12;
			return DateFormat.format(c.getString(tifo), endTime).toString();
		}
	}

	public TimeSliceCategory getCategory() {
		return category;
	}

	public void setCategory(TimeSliceCategory category) {
		this.category = category;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public long getEndTime() {
		return endTime;
	}

	public void setEndTime(long endTime) {
		this.endTime = endTime;
	}

	public String getTitleWithDuration(Context c) {
		return getCategory().getCategoryName() + ": " + getStartTimeStr(c) + " - " + getEndTimeStr(c)
				+ " (" + DateTimeFormatter.hrColMinColSec(getDurationInMilliseconds(), true) + ")";
	}

	public String getTitle(Context c) {
		return getCategory().getCategoryName() + ": " + getStartTimeStr(c) + " - " + getEndTimeStr(c);
	}

	public String getNotes() {
		if (notes != null) {
			return notes;
		} else {
			return "";
		}
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

}
